<template>
  <div class="app-container">
    <el-card shadow="never" v-loading="loading">
      <el-row :gutter="5">
        <el-col :span="24" class="main-card-view">
          <el-row>
            <el-card shadow="never" class="top-search-view-card">
              <el-row>
                <el-form :inline="true" :model="searchForm" class="demo-form-inline search-view">
                  <el-form-item label="关键字">
                    <el-input v-model="searchForm.festivalName" placeholder="请输入关键字"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="handleSearch" class="search-button">查询
                    </el-button>
                    <!--                                        <el-button type="success" @click="handleReset" class="reset-button">重置</el-button>-->
                    <el-button type="primary" @click="showCreateDialog" class="new-button">添加
                    </el-button>
                  </el-form-item>
                </el-form>
              </el-row>
            </el-card>
          </el-row>

          <el-row>
            <el-card shadow="never">
              <el-table :data="tableData" :stripe="true" style="width: 100%">

                <el-table-column prop="festivalName" align="center" label="节日名称" />
                <el-table-column prop="festivalTime" align="center" label="节日日期" />
                <el-table-column prop="createTime" align="center" label="创建时间" />
                <el-table-column prop="contentsNum" align="center" label="内容数">
                  <template slot-scope="scope">
                    <el-button @click="goReal(scope.row)" type="text">{{
                      scope.row.contentsNum
                    }}
                    </el-button>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="操作" width="200">
                  <template slot-scope="scope">
                    <el-button type="text">查看</el-button>
                    <el-button type="text" class="table-edit-btn"
                      @click="showEditDialog(scope.$index, scope.row)"></el-button>
                    <!--编辑-->
                    <el-button type="text" class="table-delete-btn"
                      @click="handleDelete(scope.$index, scope.row)"></el-button><!--删除-->
                  </template>
                </el-table-column>
              </el-table>
              <XPagination ref="pageInfo" :total="total" :get-data="getList" />
            </el-card>
          </el-row>
        </el-col>
      </el-row>
    </el-card>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form label-width="120px" ref="form" :model="form" :rules="formRules">
        <el-form-item label="节日名称" prop="festivalName">
          <el-input v-model="form.festivalName" />
        </el-form-item>
        <el-form-item label="节日日期" prop="festivalTime">
          <el-date-picker v-model="form.festivalTime" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="节日时间说明" prop="explains">
          <el-input v-model="form.explains" />
        </el-form-item>
        <el-form-item label="图片" prop="imgUrl">
          <x-upload v-model="form.imgUrl" />
        </el-form-item>
        <el-form-item label="简介" prop="intro">
          <el-input :autosize="{ minRows: 4, maxRows: 6 }" type="textarea" v-model="form.intro" />
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import XPagination from "../../components/xpagination/XPagination";
import XUpload from "../../components/xupload/XUpload"
import * as healthAwarenessDay from "../../api/healthAwarenessDay"
import * as formUtils from "../../utils/formUtils";

export default {
  name: "list",
  components: { XUpload, XPagination },
  data () {
    return {
      userInfo:{},
      searchForm: {
        festivalName: ""
      },
      tableData: [],
      loading: false,
      dialogVisible: false,
      total: 0,
      dialogTitle: "添加节日",
      form: {},
      editStatus: "add",
      formRules: {
        roleName: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
        description: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
      }


    }
  },
  methods: {
    getList (pageInfo) {
      const param = {
        ...this.searchForm,
        ...pageInfo
      }
      healthAwarenessDay.queryByPage(param).then(response => {
        console.log(response)
        this.tableData = response.data.data.rows
        this.total = response.data.data.total
        this.loading = false
      }).catch(error => {
        console.log(error)
      })
    },
    handleDelete (index, data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        healthAwarenessDay.deleteById(data.uid).then(result => {
          this.loading = false
          this.$message.success(result.data.msg)
          this.handleSearch()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    },
    handleSearch () {
      this.$refs.pageInfo.loadData()
    },
    showCreateDialog () {
      this.dialogVisible = true;
      this.dialogTitle = "添加节日"
      this.editStatus = "add"
      this.form = {}
    },
    handleSave () {
      if (this.editStatus === "add") {
        this.handleAdd()
      } else {
        this.handleEdit()
      }
    },
    handleAdd () {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return false
        }
        const param = Object.assign({}, this.form)
        param.publisherUid=this.userInfo.uid
        param.publisher=this.userInfo.userName
        param.roleUid=this.userInfo.roleUid
        healthAwarenessDay.insert(param).then(result => {
          this.$message.success(result.data.msg)
          this.dialogVisible = false
          this.handleSearch()
        }).catch(error => {
          this.$message.error(error)
          console.log(error)
        })

      })
    },
    handleEdit () {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return false
        }
        const param = Object.assign({}, this.form)
        param.publisherUid=this.userInfo.uid
        param.publisher=this.userInfo.userName
        param.roleUid=this.userInfo.roleUid
        delete param.createTime
        delete param.updateTime
        healthAwarenessDay.update(param).then(result => {
          this.$message.success(result.data.msg)
          this.dialogVisible = false
          this.handleSearch()
        }).catch(error => {
          this.$message.error(error)
          console.log(error)
        })
      })
    },
    showEditDialog (index, data) {
      this.form = Object.assign({}, data)
      this.dialogVisible = true
      this.dialogTitle = "编辑节日"
      this.editStatus = "edit"
    },
    goReal (row) {
      this.$router.push({
        path: '/publicityActivity/propagandaContent',
        query: {
          ptUid:row.uid
        }
      })
    }
  },
  mounted () {
    this.userInfo = JSON.parse(localStorage.getItem('user_info'))
    this.handleSearch()
  }
}
</script>

<style scoped lang="scss"></style>
